//
//  GQHQutationDetailController.m
//  Xanthium
//
//  Created by GuanQinghao on 12/04/2018.
//  Copyright © 2018 GuanQinghao. All rights reserved.
//

#pragma mark Macro
#import "GQHHeader.h"

#pragma mark Model
#import "GQHQutationModel.h"

#pragma mark Object

#pragma mark View
#import "GQHQutationDetailView.h"

#pragma mark Controller
#import "GQHQutationDetailController.h"
#import "GQHContractLoadingInfoController.h"

#pragma mark CocoaPods

#pragma mark ----------Header End----------

@interface GQHQutationDetailController () <GQHQutationDetailViewDelegate>{
    
#pragma mark Data
    /// 请求参数
    NSMutableDictionary *parameters;
    /// 返回模型数组
    __block NSMutableArray<GQHQutationModel *> *qutationModelArray;
#pragma mark View
    /// 自定义根视图
    GQHQutationDetailView *rootView;
}

@end


@implementation GQHQutationDetailController

#pragma mark --Lifecycle
- (void)loadView {
    DLog();
    
    rootView = [[GQHQutationDetailView alloc] initWithFrame:[UIScreen mainScreen].bounds];
    rootView.qh_delegate = self;
    self.view = rootView;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    DLog();
    
    [self loadUserInterface];
    
    parameters = [NSMutableDictionary dictionary];
    [parameters setObject:_qh_freightModel.qh_freightID forKey:@"sourcesId"];
    
    [self fetchServiceDataWithParameters:parameters];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    DLog();
    
    self.qh_titleLabel.text = @"报价信息";
    self.qh_rightButton.hidden = YES;
    self.automaticallyAdjustsScrollViewInsets = NO;
    
    [rootView.qh_tableView.mj_header beginRefreshing];
}

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    DLog();
    
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    
    [rootView.qh_tableView.mj_header endRefreshing];
}

#pragma mark --Delegate
- (void)qh_createOrCheckOrCancelContract:(UIButton *)sender {
    DLog(@"发送或查看或取消协议");
    
    GQHQutationModel *qutationModel = qutationModelArray[sender.tag];
    
    if ([_qh_state isEqualToString:@"01"] && [qutationModel.qh_quoteState isEqualToString:@"01"]) {
        /// 已报价状态 (发送协议)
        GQHContractLoadingInfoController *contractLoadingInfoController = [[GQHContractLoadingInfoController alloc] init];
        contractLoadingInfoController.qh_qutationModel = qutationModel;
        contractLoadingInfoController.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:contractLoadingInfoController animated:YES];
    } else if ([_qh_state isEqualToString:@"01"] && [qutationModel.qh_quoteState isEqualToString:@"02"]){
        /// 已中标状态 (撤回协议)
        NSString *APIURLString = [[NSString qh_APIURLString] stringByAppendingPathComponent:@"/buWaybillAgreement/1/cancel"];
        
        NSMutableDictionary *para = [NSMutableDictionary dictionary];
        [para setObject:qutationModel.qh_qutationID forKey:@"id"];
        
        [GQHNetHelper qh_requestGET:APIURLString parameters:para success:^(NSNumber *code, NSString *message, NSDictionary *data) {
            DLog(@"%@---%@---%@",code,message,data);
            
            if (0 == [code integerValue]) {
                
                [rootView.qh_tableView.mj_header beginRefreshing];
            } else {
                
                [self showToastWithText:message];
            }
        } failure:^(NSError *error) {
            
            NSString *errorString = [NSString stringWithFormat:@"网络出错:%@,code:%ld", error.domain, (long)error.code];
            [self showToastWithText:errorString];
        }];
    } else if ([qutationModel.qh_quoteState isEqualToString:@"03"]) {
        /// 已承运状态 (查看协议)
        
        GQHContractLoadingInfoController *contractLoadingInfoController = [[GQHContractLoadingInfoController alloc] init];
        contractLoadingInfoController.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:contractLoadingInfoController animated:YES];
        
    } else {
        
        [self qh_alertWithTitle:nil message:@"请上架货源后再进行操作" actionTitle:nil
                        handler:nil completion:nil];
    }
}

- (void)qh_dialMobile:(UIButton *)sender {
    DLog(@"拨打电话");
    
    GQHQutationModel *qutationModel = qutationModelArray[sender.tag];
    [self qh_dialWithMobile:qutationModel.qh_quoterMobile];
}

#pragma mark --TargetMethod

#pragma mark --PrivateMethod
- (void)fetchServiceDataWithParameters:(NSMutableDictionary *)parameters {
    
    __block int page = 1;
    
    [parameters setObject:[NSNumber numberWithInt:page] forKey:@"page"];
    [parameters setObject:ROWS forKey:@"rows"];
    
    rootView.qh_tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        
        page = 1;
        [parameters setObject:[NSNumber numberWithInt:page] forKey:@"page"];
        
        [GQHQutationModel qh_fetchQutationsWithParameters:parameters success:^(BOOL status, NSNumber *code, NSString *message, NSArray<GQHQutationModel *> *qutations) {
            
            if (status) {
                
                qutationModelArray = [NSMutableArray arrayWithArray:qutations];
                rootView.qh_qutationModelArray = qutationModelArray;
                
                [rootView.qh_tableView reloadData];
                
            } else {
                
                [self showToastWithText:message];
            }
            [rootView.qh_tableView.mj_header endRefreshing];
        } failure:^(NSError *error) {
            
            NSString *errorString = [NSString stringWithFormat:@"网络出错:%@,code:%ld", error.domain, (long)error.code];
            [self showToastWithText:errorString];
            [rootView.qh_tableView.mj_header endRefreshing];
        }];
    }];
    
    rootView.qh_tableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
        
        page++;
        [parameters setObject:[NSNumber numberWithInt:page] forKey:@"page"];
        
        [GQHQutationModel qh_fetchQutationsWithParameters:parameters success:^(BOOL status, NSNumber *code, NSString *message, NSArray<GQHQutationModel *> *qutations) {
            
            if (status) {
                
                if (qutations && qutations.count > 0) {
                    
                    [qutationModelArray addObjectsFromArray:qutations];
                    rootView.qh_qutationModelArray = qutationModelArray;
                    [rootView.qh_tableView reloadData];
                    [rootView.qh_tableView.mj_footer endRefreshing];
                } else {
                    
                    [rootView.qh_tableView.mj_footer endRefreshingWithNoMoreData];
                }
            } else {
                
                [self showToastWithText:message];
                [rootView.qh_tableView.mj_footer endRefreshing];
            }
        } failure:^(NSError *error) {
            
            NSString *errorString = [NSString stringWithFormat:@"网络出错:%@,code:%ld", error.domain, (long)error.code];
            [self showToastWithText:errorString];
            [rootView.qh_tableView.mj_footer endRefreshing];
        }];
    }];
}

#pragma mark --Setter

#pragma mark --Getter

#pragma mark --View
- (void)loadUserInterface {
    
    UIView *containerView = [[UIView alloc] init];
    containerView.backgroundColor = kLightGrayColor;
    [rootView addSubview:containerView];
    [containerView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.mas_equalTo(rootView).with.inset(rootView.qh_statusBarHeight + rootView.qh_navigationBarHeight);
        make.left.and.right.mas_equalTo(rootView);
        make.height.mas_equalTo(145);
    }];
    
    ///
    UIView *supplyView = [[UIView alloc] init];
    supplyView.backgroundColor = kAppRedColor;
    [containerView addSubview:supplyView];
    [supplyView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.mas_equalTo(containerView);
        make.left.and.right.mas_equalTo(containerView);
        make.height.mas_equalTo(80);
    }];
    
    UILabel *routeLabel = [[UILabel alloc] init];
    routeLabel.backgroundColor = kClearColor;
    routeLabel.font = kDefaultFont;
    routeLabel.text = [NSString stringWithFormat:@"%@ - %@",_qh_freightModel.qh_departure,_qh_freightModel.qh_destination];
    routeLabel.textColor = kWhiteColor;
    [supplyView addSubview:routeLabel];
    [routeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.mas_equalTo(supplyView);
        make.left.and.right.mas_equalTo(supplyView).with.inset(GQH_MARGIN_SINGLE);
        make.height.mas_equalTo(30);
    }];
    
    UILabel *contentLabel = [[UILabel alloc] init];
    contentLabel.backgroundColor = kClearColor;
    contentLabel.font = kSmallerFont;
    contentLabel.text = [NSString stringWithFormat:@"%@/%@/%@",_qh_freightModel.qh_freightName,_qh_freightModel.qh_modelWanted,_qh_freightModel.qh_lengthWanted];
    contentLabel.textColor = kWhiteColor;
    [supplyView addSubview:contentLabel];
    [contentLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.mas_equalTo(routeLabel.mas_bottom);
        make.left.and.right.mas_equalTo(routeLabel);
        make.height.mas_equalTo(20);
    }];
    
    UILabel *timeLabel = [[UILabel alloc] init];
    timeLabel.backgroundColor = kClearColor;
    timeLabel.font = kSmallerFont;
    timeLabel.text = [NSString stringWithFormat:@"装车时间: %@",_qh_freightModel.qh_loadingTime];
    timeLabel.textColor = kWhiteColor;
    [supplyView addSubview:timeLabel];
    [timeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.mas_equalTo(contentLabel.mas_bottom);
        make.height.and.left.and.right.mas_equalTo(contentLabel);
    }];
    
    ///
    UIView *qutationView = [[UIView alloc] init];
    qutationView.backgroundColor = kWhiteColor;
    [containerView addSubview:qutationView];
    [qutationView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.mas_equalTo(supplyView.mas_bottom);
        make.left.and.right.mas_equalTo(supplyView);
        make.height.mas_equalTo(60);
    }];
    
    UILabel *quoterLabel = [[UILabel alloc] init];
    quoterLabel.font = kDefaultFont;
    quoterLabel.text = [NSString stringWithFormat:@"%d人",_qh_freightModel.qh_quotationCount];
    quoterLabel.textColor = kDarkBlackColor;
    quoterLabel.textAlignment = NSTextAlignmentCenter;
    [qutationView addSubview:quoterLabel];
    [quoterLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.mas_equalTo(10);
        make.left.mas_equalTo(qutationView);
        make.right.mas_equalTo(qutationView.mas_centerX);
        make.height.mas_equalTo(20);
    }];
    
    UILabel *quoterTitleLabel = [[UILabel alloc] init];
    quoterTitleLabel.font = kSmallerFont;
    quoterTitleLabel.text = @"提供报价人数";
    quoterTitleLabel.textColor = kDarkBlackColor;
    quoterTitleLabel.textAlignment = NSTextAlignmentCenter;
    [qutationView addSubview:quoterTitleLabel];
    [quoterTitleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.mas_equalTo(quoterLabel.mas_bottom);
        make.left.and.right.and.height.mas_equalTo(quoterLabel);
    }];
    
    UILabel *carrierLabel = [[UILabel alloc] init];
    carrierLabel.font = kDefaultFont;
    carrierLabel.text = [NSString stringWithFormat:@"%d人",_qh_freightModel.qh_agreementCount];
    carrierLabel.textColor = kDarkBlackColor;
    carrierLabel.textAlignment = NSTextAlignmentCenter;
    [qutationView addSubview:carrierLabel];
    [carrierLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.and.height.mas_equalTo(quoterLabel);
        make.left.mas_equalTo(qutationView.mas_centerX);
        make.right.mas_equalTo(qutationView);
    }];
    
    UILabel *carrierTitleLabel = [[UILabel alloc] init];
    carrierTitleLabel.font = kSmallerFont;
    carrierTitleLabel.text = @"确认承运人数";
    carrierTitleLabel.textColor = kDarkBlackColor;
    carrierTitleLabel.textAlignment = NSTextAlignmentCenter;
    [qutationView addSubview:carrierTitleLabel];
    [carrierTitleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.mas_equalTo(carrierLabel.mas_bottom);
        make.left.and.right.and.height.mas_equalTo(carrierLabel);
    }];
    
    UIView *separatorView = [[UIView alloc] init];
    separatorView.backgroundColor = kLightGrayColor;
    [qutationView addSubview:separatorView];
    [separatorView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.size.mas_equalTo(CGSizeMake(2.0f, 40.0f));
        make.center.mas_equalTo(qutationView);
    }];
}

@end
